{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import glob\n",
    "import os\n",
    "import tifffile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_list_of_files(address):\n",
    "    \n",
    "    files_c = glob.glob(os.path.join(address,\"*_CLS.tif\"))\n",
    "    files_h = glob.glob(os.path.join(address,\"*_AGL.tif\"))\n",
    "    \n",
    "    return sorted(files_c),sorted(files_h)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f_c1,f_h1=get_list_of_files('/data/DW/DataFusion/dfc2019-master/TRACK1/Submission5/')\n",
    "f_c2,f_h2=get_list_of_files('/data/DW/DataFusion/dfc2019-master/TRACK1/Submission51/')\n",
    "f_c3,f_h3=get_list_of_files('/data/DW/DataFusion/dfc2019-master/TRACK1/Submission52/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "output_path='/data/DW/DataFusion/dfc2019-master/TRACK1/test_sub5/'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Segmentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 50/50 [02:23<00:00,  3.01s/it]\n"
     ]
    }
   ],
   "source": [
    "temp_c=np.zeros([3,1024,1024]).astype(int)\n",
    "\n",
    "for i in tqdm(range(len(f_c1))):\n",
    "    \n",
    "    temp_c[0]=tifffile.imread(f_c1[i])\n",
    "    temp_c[1]=tifffile.imread(f_c2[i])\n",
    "    temp_c[2]=tifffile.imread(f_c3[i])\n",
    "\n",
    "    \n",
    "    vote_c_label=np.zeros([1024,1024])\n",
    "    for j in range(vote_c_label.shape[0]):\n",
    "        for k in range(vote_c_label.shape[1]):\n",
    "            vote_c_label[j,k]=np.argmax(np.bincount(temp_c[:,j,k]))\n",
    "            \n",
    "    _,c_name=os.path.split(f_c1[i])\n",
    "    tifffile.imsave(os.path.join(output_path+c_name),vote_c_label)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "  0%|          | 0/50 [00:00<?, ?it/s]\u001b[A\n",
      "  2%|▏         | 1/50 [00:00<00:14,  3.32it/s]\u001b[A\n",
      "  4%|▍         | 2/50 [00:00<00:13,  3.61it/s]\u001b[A\n",
      "  6%|▌         | 3/50 [00:00<00:12,  3.84it/s]\u001b[A\n",
      "  8%|▊         | 4/50 [00:00<00:11,  4.04it/s]\u001b[A\n",
      " 10%|█         | 5/50 [00:01<00:10,  4.18it/s]\u001b[A\n",
      " 12%|█▏        | 6/50 [00:01<00:10,  4.10it/s]\u001b[A\n",
      " 14%|█▍        | 7/50 [00:01<00:10,  4.29it/s]\u001b[A\n",
      " 16%|█▌        | 8/50 [00:01<00:09,  4.32it/s]\u001b[A\n",
      " 18%|█▊        | 9/50 [00:02<00:09,  4.29it/s]\u001b[A\n",
      " 20%|██        | 10/50 [00:02<00:09,  4.24it/s]\u001b[A\n",
      " 22%|██▏       | 11/50 [00:02<00:08,  4.38it/s]\u001b[A\n",
      " 24%|██▍       | 12/50 [00:02<00:08,  4.70it/s]\u001b[A\n",
      " 26%|██▌       | 13/50 [00:02<00:07,  4.79it/s]\u001b[A\n",
      " 28%|██▊       | 14/50 [00:03<00:07,  4.75it/s]\u001b[A\n",
      " 30%|███       | 15/50 [00:03<00:07,  4.94it/s]\u001b[A\n",
      " 32%|███▏      | 16/50 [00:03<00:07,  4.49it/s]\u001b[A\n",
      " 34%|███▍      | 17/50 [00:03<00:07,  4.40it/s]\u001b[A\n",
      " 36%|███▌      | 18/50 [00:04<00:06,  4.62it/s]\u001b[A\n",
      " 38%|███▊      | 19/50 [00:04<00:06,  4.76it/s]\u001b[A\n",
      " 40%|████      | 20/50 [00:04<00:06,  4.69it/s]\u001b[A\n",
      " 42%|████▏     | 21/50 [00:04<00:06,  4.41it/s]\u001b[A\n",
      " 44%|████▍     | 22/50 [00:04<00:06,  4.36it/s]\u001b[A\n",
      " 46%|████▌     | 23/50 [00:05<00:06,  4.45it/s]\u001b[A\n",
      " 48%|████▊     | 24/50 [00:05<00:06,  4.31it/s]\u001b[A\n",
      " 50%|█████     | 25/50 [00:05<00:06,  4.13it/s]\u001b[A\n",
      " 52%|█████▏    | 26/50 [00:05<00:05,  4.16it/s]\u001b[A\n",
      " 54%|█████▍    | 27/50 [00:06<00:05,  4.36it/s]\u001b[A\n",
      " 56%|█████▌    | 28/50 [00:06<00:04,  4.77it/s]\u001b[A\n",
      " 58%|█████▊    | 29/50 [00:06<00:04,  4.67it/s]\u001b[A\n",
      " 60%|██████    | 30/50 [00:06<00:04,  4.54it/s]\u001b[A\n",
      " 62%|██████▏   | 31/50 [00:06<00:04,  4.58it/s]\u001b[A\n",
      " 64%|██████▍   | 32/50 [00:07<00:03,  4.63it/s]\u001b[A\n",
      " 66%|██████▌   | 33/50 [00:07<00:03,  4.90it/s]\u001b[A\n",
      " 68%|██████▊   | 34/50 [00:07<00:03,  4.84it/s]\u001b[A\n",
      " 70%|███████   | 35/50 [00:07<00:03,  4.75it/s]\u001b[A\n",
      " 72%|███████▏  | 36/50 [00:07<00:02,  4.67it/s]\u001b[A\n",
      " 74%|███████▍  | 37/50 [00:08<00:02,  4.61it/s]\u001b[A\n",
      " 76%|███████▌  | 38/50 [00:08<00:02,  4.63it/s]\u001b[A\n",
      " 78%|███████▊  | 39/50 [00:08<00:02,  4.92it/s]\u001b[A\n",
      " 80%|████████  | 40/50 [00:08<00:02,  4.90it/s]\u001b[A\n",
      " 82%|████████▏ | 41/50 [00:09<00:01,  5.07it/s]\u001b[A\n",
      " 84%|████████▍ | 42/50 [00:09<00:01,  5.32it/s]\u001b[A\n",
      " 86%|████████▌ | 43/50 [00:09<00:01,  5.31it/s]\u001b[A\n",
      " 88%|████████▊ | 44/50 [00:09<00:01,  5.21it/s]\u001b[A\n",
      " 90%|█████████ | 45/50 [00:09<00:00,  5.12it/s]\u001b[A\n",
      " 92%|█████████▏| 46/50 [00:10<00:00,  4.70it/s]\u001b[A\n",
      " 94%|█████████▍| 47/50 [00:10<00:00,  4.69it/s]\u001b[A\n",
      " 96%|█████████▌| 48/50 [00:10<00:00,  4.64it/s]\u001b[A\n",
      " 98%|█████████▊| 49/50 [00:10<00:00,  4.52it/s]\u001b[A\n",
      "100%|██████████| 50/50 [00:10<00:00,  4.66it/s]\u001b[A\n",
      "\u001b[A"
     ]
    }
   ],
   "source": [
    "temp_h=np.zeros([3,1024,1024])\n",
    "\n",
    "for i in tqdm(range(len(f_h1))):\n",
    "    \n",
    "    temp_h[0]=tifffile.imread(f_h1[i])\n",
    "    temp_h[1]=tifffile.imread(f_h2[i])\n",
    "    temp_h[2]=tifffile.imread(f_h3[i])\n",
    "\n",
    "    \n",
    "    vote_h=np.zeros([1024,1024])\n",
    "    vote_h=np.median(temp_h,axis=0)\n",
    "            \n",
    "    _,h_name=os.path.split(f_h1[i])\n",
    "    tifffile.imsave(os.path.join(output_path+h_name),vote_h)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (py4torch_tf)",
   "language": "python",
   "name": "pytorch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
